<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../css/layui.css"  media="all">
</head>
<body>
<form class="layui-form layui-form-pane">
    <div class="layui-form-item">
        <label class="layui-form-label">俗名</label>
        <div class="layui-input-block">
            <input type="text" name="name" autocomplete="off" placeholder="请输入俗名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">纲</label>
        <div class="layui-input-inline">
            <input type="text" name="gang" placeholder="请输入纲" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label">目</label>
        <div class="layui-input-inline">
            <input type="text" name="mu" placeholder="请输入目" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label">科</label>
        <div class="layui-input-inline">
            <input type="text" name="ke" placeholder="请输入科" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label">属</label>
        <div class="layui-input-inline">
            <input type="text" name="shu" placeholder="请输入属" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">记录人</label>
        <div class="layui-input-block">
            <input type="text" name="user" autocomplete="off" placeholder="请输入记录人" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test1">上传图片</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1" style="width: 200px;height: 200px">
                <p id="demoText"></p>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入描述" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit="" lay-filter="demo2">保存</button>
    </div>
</form>

<script src="../layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate','upload'], function(){
        var form = layui.form
            , $ = layui.jquery
            ,layer = layui.layer
            ,upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/upload'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        //监听提交
        form.on('submit(demo2)', function(data){
            layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })

            $.ajax({
                type: "POST",
                url:  "/api/botany",
                contentType: "application/json",
                data: JSON.stringify(JSON.stringify(data.field)),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){

                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
            return false;
        });


    });
</script>

</body>
</html>